
import DealRoom from "../../Entities/DealRoom";

cc.Class({
    extends: cc.Component,

    properties: {
        itemModel:{
            default:null,
            type:cc.Prefab
        },

        contentNode:{
            default:null,
            type:cc.Node
        },

        titleParent:{
            default:null,
            type:cc.Node
        },

        totalParent:{
            default:null,
            type:cc.Node,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    // update (dt) {},

    refreshDealBoardSv:function (scores) {
        scores = scores || cc.gg.dataMgr.room.scores
        cc.log("刷新计分板：",scores)

        this._clearAllScore()
        this._setName(scores)

        let allStrs = this._getAllStrs(scores)
        for(let i=0;i<allStrs.length;i++){
            let strs = allStrs[i]

            let itemNode = this.getItemByDealIndex(i)
            itemNode.getComponent("DealBoardItem").initDealBoardItem(i+1,strs)
        }

        let totals = this._getTotalScores(scores)
        for(let i=0;i<totals.length;i++){
            let tScore = totals[i]
            this.totalParent.children[i].active = true
            this.totalParent.children[i].getComponent(cc.Label).string = tScore
        }
    },

    clearDealBoard:function () {
        this.contentNode.removeAllChildren(true)
        // for(let i=0;i<this.contentNode.children.length;i++){
        //     this.contentNode.children[i].active = false
        //     this.contentNode.children[i].removeFromParent(true)
        // }
    },

    _getTotalScores:function (scores) {
        let totals = []
        for (var key in scores){
            let oneTaltal = 0
            let oneScores = scores[key].scores
            for(let i=0;i<oneScores.length;i++){
                oneTaltal += oneScores[i]
            }
            if(cc.gg.dataMgr.room instanceof DealRoom)
                oneTaltal += cc.gg.dataMgr.room.baseScore
            totals.push(oneTaltal)
        }
        return totals
    },

    _getAllStrs:function (allScores) {
        if(!allScores.length) return []

        let firstScores = null
        let uids = []
        for (var key in allScores){
            firstScores = allScores[key]
            break
        }
        for (let i=0;i<firstScores.length;i++){
            let scoreObj = firstScores[i]
            uids.push(scoreObj.id)
        }

        let allStrs = []
        let scoreRow = 0
        for(let x in allScores){
            let curScores = allScores[x].scores.length
            if(!scoreRow){
                scoreRow = curScores
                continue
            }

            scoreRow = scoreRow > curScores ? scoreRow : curScores
        }
        cc.log("一共有几次记录：",scoreRow)
        for(let userScoreIdx=0;userScoreIdx<scoreRow;userScoreIdx++){
            let strs = []

            // let rowObjs = this._getObjByIdx(userScoreIdx,allScores)
            // for(let j=0;j<uids.length;j++){
            //     let id = uids[userScoreIdx]
            //     let netObj = this._getScoreObj(id,rowObjs)
            //     strs.push(netObj.score || "")
            // }


            for(let id in allScores){
                id = parseInt(id)
                let userScores = allScores[id].scores
                let score = userScores[userScoreIdx]
                if(score){
                    strs.push(score)
                }else if(score !== 0){
                    strs.push("")
                }else strs.push(0)
            }

            // let exists = false
            // for(let id in allScores){
            //     id = parseInt(id)
            //     if(scoreObj.id === id){
            //         exists = true
            //         strs.push(allScores[id].scores[i])
            //     }
            // }
            // if(!exists) strs.push("")

            allStrs.push(strs)
            cc.log("最后的strs：",strs)
        }

        return allStrs
    },

    _getObjByIdx:function (idx, map) {
        let i = 0
        for(let x in map){
            let obj = map[x]
            if(idx === i){
                return obj
            }
            i++
        }
        return null
    },

    _getScoreObj:function (id, fromArray) {
        for(let i=0;i<fromArray.length;i++){
            let netObj = fromArray[i]
            if(netObj.id === id){
                return netObj
            }
        }
        return null
    },

    _getIdxUidMap:function () {
        if(!this._idxUIDMap){
            let firstLine = null
            for(let id in cc.gg.dataMgr.room.scores){
                firstLine = cc.gg.dataMgr.room.scores[id]
                break
            }
            this._idxUIDMap = []
            for(let i=0;i<firstLine.length;i++){
                this._idxUIDMap[i] = firstLine[i].id
            }
        }

        return this._idxUIDMap
    },

    _setName:function (scores) {
        let nodeIdx = 0
        for(let key in scores){
            let id = parseInt(key)
            let user = cc.gg.dataMgr.getUser(id)
            if(!user){
                cc.error("No user with id:",id)
                return
            }
            let titleNode = this.titleParent.children[nodeIdx++]
            titleNode.active = true
            titleNode.getComponent(cc.Label).string = user.name
        }
    },

    _clearAllScore:function () {
        for(let i=0;i<this.titleParent.children.length;i++){
            this.titleParent.children[i].active = false
        }

        for(let i=0;i<this.totalParent.children.length;i++){
            this.totalParent.children[i].active = false
        }
    },

    getItemByDealIndex:function (idx) {
        for(let i=0; i<this.contentNode.children.length; i++){
            let itemNode = this.contentNode.children[i]
            if(itemNode.idx === idx) return itemNode
        }

        let itemNode = cc.instantiate(this.itemModel)
        this.contentNode.addChild(itemNode)
        itemNode.idx = idx

        return itemNode
    },
});
